package oocl.ita.eordering.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import oocl.ita.eordering.dao.ClientDao;
import oocl.ita.eordering.po.Client;
import oocl.ita.eordering.po.Order;

@Repository("cDao")
public class ClientDaoImpl implements ClientDao {
	@PersistenceContext(name = "un") // auwire 的子类
	private EntityManager em;

	@Override
	// @Transactional
	public int addClient(Client c) {
		em.persist(c);
		return c.getcId();
	}

	@Override
	public Client findClient(String tel, String psd) {

		String jpql = "select c from Client c where c.cTel=:cTel and c.cPassword=:cPassword";
		@SuppressWarnings("unchecked")
		List<Client> ls = em.createQuery(jpql).setParameter("cTel", tel).setParameter("cPassword", psd).getResultList();
		if (ls.isEmpty())
			return null;
		else
			return ls.get(0);

	}

	@Override
	public int updateClient(Client client) {
		// TODO Auto-generated method stub
		Client dbClient = em.find(Client.class, client.getcId());
		dbClient.setcLocation(client.getcLocation());
		dbClient.setcPassword(client.getcPassword());
		dbClient.setcPersonName(client.getcPersonName());
		return 1;
	}
}
